package main;

import main.Raycast.ColorMode;
import main.Raycast.ColorVariation;

public class ArgumentParser {
    enum Parameter { 
        SCENE("-i"),
        OUTPUT("-o"),
        SIZE("-size"),
        FOV("-fov"),
        COLOR_MODE("-cm"),
        COLOR_VARIATION("-cv"),
        TIME("-time"),
        PROGRESS("-progress"),
        INVALID_PARAMETER("");

        String str;
        Parameter(String str) {
            this.str = str;
        }

        static public Parameter getEnum(String str) {
            for (Parameter p : Parameter.values())
                if (p.str.compareTo(str) == 0)
                    return p;
            
            return INVALID_PARAMETER;
        }
    };
    
    static boolean parse(String[] args) {
        for (int i=0; i<args.length; i++) {
            switch(Parameter.getEnum(args[i])) {
            case SCENE:
                if (++i >= args.length)
                    return false;

                Raycast.sceneFile = args[i];
                break;

            case OUTPUT:
                if (++i >= args.length)
                    return false;

                Raycast.outputFile = args[i];
                String copy = String.copyValueOf(Raycast.outputFile.toCharArray()).toLowerCase();
                if (copy.endsWith(".bmp"))
                    Raycast.outputType = "bmp";
                break;

            case SIZE:
                if (++i >= args.length)
                    return false;

                try {
                    int div = args[i].indexOf("x");
                    Raycast.width = Integer.parseInt(args[i].substring(0, div));
                    Raycast.height =  Integer.parseInt(args[i].substring(div + 1, args[i].length()));
                } catch (Exception e) {
                    return false;
                }
                break;

            case FOV:
                if (++i >= args.length)
                    return false;

                try {
                    Raycast.fov = Double.parseDouble(args[i].substring(0, args[i].length()));
                } catch (Exception e) {
                    return false;
                }
                break;

            case COLOR_MODE:
                if (++i >= args.length)
                    return false;

                if (args[i].compareTo("random") == 0) {
                    Raycast.colorMode = ColorMode.Random;
                } else if (args[i].compareTo("ordered") == 0) {
                    Raycast.colorMode = ColorMode.Ordered;
                } else
                    return false;
                
                break;
            case COLOR_VARIATION:
                if (++i >= args.length)
                    return false;

                if (args[i].compareTo("linear") == 0) {
                    Raycast.colorVariation = ColorVariation.Linear;
                } else if (args[i].compareTo("log") == 0) {
                    Raycast.colorVariation = ColorVariation.Logarithmic;
                } else
                    return false;

                break;

            case TIME:
                Raycast.countTime = true;
                break;
            case PROGRESS:
                Raycast.progress = true;
                break;
            case INVALID_PARAMETER:
                return false;
            }
        }

        if (Raycast.sceneFile == null)
            return false;
        return true;
    }
}